package com.jnmd.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jnmd.pojo.Admin;
import com.jnmd.service.AdminService;
import com.jnmd.utils.MD5;

@Controller
@RequestMapping("/admin")
public class LoginController {

	@Autowired
	private AdminService adminService;
	
	// 跳转登陆界面
	@RequestMapping({"loginView.action","index.action"})
	public String login(){
		return "admin/login";
	}
	
	// 实现登录功能
	@RequestMapping("/login.action")
	public String login(Admin admin,HttpSession session) throws Exception {
		if((!admin.getUsername().equals(""))){
			
			// MD5加密
			admin.setPassword(MD5.md5JiaMi(admin.getPassword()));
			
			List<Admin> list = adminService.queryAdmin(admin);
			
			if(list.size()!=0){
				session.setAttribute("userName", list.get(0).getUsername());
				
				return "redirect:/videoList.action";
			}else{
				return "admin/login";
			}
		}else{
			return "redirect:/admin/index.action";
		}
	}
	
	@RequestMapping("exit.action")
	public String exit(HttpSession session){
		session.removeAttribute("userName");
		return "admin/login";
	}
	
}
